<?php
/**
 * User: xiaolihai
 * Date: 2020-5-9
 * Time: 12:30
 */

namespace App\Services;
use Illuminate\Support\Facades\Log;
//实名认证
class RealNameService extends BaseService
{
    /**
     * 实名认证
     * @param array $data
     * @return array
     */
    public static function realName($data=array())
    {
        Log::info('实名认证参数：', $data);
        header('Content-type:text/html;charset=utf-8');
        $appkey = "3d5cdf7a9a61b54d6a3b00bc2a33bebf";
        //************1.真实姓名和身份证号码判断是否一致************
        $url = "http://op.juhe.cn/idcard/query";
        $params = array(
            "idcard" => $data['idcard'],//身份证号码
            "realname" => $data['realname'],//真实姓名
            "key" => $appkey,//应用APPKEY(应用详细页查询)
        );

        $paramstring = http_build_query($params);
        $content = self::juhecurl($url,$paramstring);
        $result = json_decode($content,true);
        if($result){
            if($result['error_code']=='0'){
                if($result['result']['res'] == '1'){
                    $return_data = array(
                        'status'=>0,
                        'msg'=> '身份证号码和真实姓名'
                    );
                    return $return_data;
                }else{
                    $return_data = array(
                        'status'=>-1,
                        'msg'=> '身份证号码和真实姓名不一致'
                    );
                    return $return_data;
                }
            }else{
                $return_data = array(
                    'status'=>-1,
                    'msg'=> $result['reason']
                );
                return $return_data;
            }
        }else{
            $return_data = array(
                'status'=>-1,
                'msg'=> '请求失败'
            );
            return $return_data;
        }
    }
    /**
     * 请求接口返回内容
     * @param  string $url [请求的URL地址]
     * @param  string $params [请求的参数]
     * @param  int $ipost [是否采用POST形式]
     * @return  string
     */
    public static function juhecurl($url,$params=false,$ispost=0){
        $httpInfo = array();
        $ch = curl_init();

        curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
        curl_setopt( $ch, CURLOPT_USERAGENT , 'JuheData' );
        curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 );
        curl_setopt( $ch, CURLOPT_TIMEOUT , 60);
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        if( $ispost )
        {
            curl_setopt( $ch , CURLOPT_POST , true );
            curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
            curl_setopt( $ch , CURLOPT_URL , $url );
        }
        else
        {
            if($params){
                curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
            }else{
                curl_setopt( $ch , CURLOPT_URL , $url);
            }
        }
        $response = curl_exec( $ch );
        if ($response === FALSE) {
            //echo "cURL Error: " . curl_error($ch);
            return false;
        }
        $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
        $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
        curl_close( $ch );
        return $response;
    }

    public static function isCreditNo($vStr){
        $vCity = array(
            '11','12','13','14','15','21','22',
            '23','31','32','33','34','35','36',
            '37','41','42','43','44','45','46',
            '50','51','52','53','54','61','62',
            '63','64','65','71','81','82','91'
        );

        if (!preg_match('/^([\d]{17}[xX\d]|[\d]{15})$/', $vStr))

            return false;

        if (!in_array(substr($vStr, 0, 2), $vCity))

            return false;

        $vStr = preg_replace('/[xX]$/i', 'a', $vStr);
        $vLength = strlen($vStr);
        if ($vLength == 18) {
            $vBirthday = substr($vStr, 6, 4) . '-' . substr($vStr, 10, 2) . '-' . substr($vStr, 12, 2);
        } else {
            $vBirthday = '19' . substr($vStr, 6, 2) . '-' . substr($vStr, 8, 2) . '-' . substr($vStr, 10, 2);
        }

        if (date('Y-m-d', strtotime($vBirthday)) != $vBirthday)

            return false;

        if ($vLength == 18) {
            $vSum = 0;
            for ($i = 17 ; $i >= 0 ; $i--) {
                $vSubStr = substr($vStr, 17 - $i, 1);
                $vSum += (pow(2, $i) % 11) * (($vSubStr == 'a') ? 10 : intval($vSubStr , 11));
            }
            if($vSum % 11 != 1) return false;
        }
        return true;
    }

}